package se.teamscape.client.view;

import java.util.ArrayList;

import se.teamscape.client.controller.AppController;
import se.teamscape.client.presenter.EditDataPresenter;
import se.teamscape.client.utils.NameValuePair;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Generic view for editing a name / textfield pair in a table.
 * Adds a Save and Cancel button for user interaction.
 * 
 * @author Andreas Borglin
 * 
 */
public class EditDataView extends BaseView implements EditDataPresenter.Display {

    private FlexTable mTable;
    private FlowPanel mPanel;
    private Button mSaveButton;
    private Button mCancelButton;

    public EditDataView() {
    }

    @Override
    public void init() {

        mTable = new FlexTable();
        mPanel = new FlowPanel();
        mSaveButton = new Button();
        mCancelButton = new Button();

        mSaveButton.setText(AppController.getConstants().save());
        mCancelButton.setText(AppController.getConstants().cancel());
        mSaveButton.setStyleName("HorizontalFlow");
        mCancelButton.setStyleName("HorizontalFlow");

        mPanel.add(mTable);
        mPanel.add(mSaveButton);
        mPanel.add(mCancelButton);

        initWidget(mPanel);
    }

    @Override
    public HasClickHandlers getCancel() {
        return mCancelButton;
    }

    @Override
    public HasClickHandlers getSave() {
        return mSaveButton;
    }

    @Override
    public void setData(ArrayList<NameValuePair<String>> data) {

        int row = 0, col = 0;
        for (NameValuePair<String> pair : data) {

            mTable.setText(row, col++, pair.getName());
            TextBox field = new TextBox();
            field.setText(pair.getValue());
            field.setMaxLength(30);
            mTable.setWidget(row++, col--, field);

        }

    }

}